package com.lyy.mapper;

import com.lyy.entity.Student;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Liangyaoyun
 */
@Repository
public interface StudentMapper {
    String studentList = "select student_id,real_name,gender,phone,deleted,clazz_id,create_time,modify_time from  springboot.student s";
    String deleteBystudentId = "delete from springboot.student where student_id=#{studentId}";

    String updateBystudentId = "<script> update springboot.student <set> " +
            "<if test='studentId!=null'>student_id=#{studentId},</if>" +
            "<if test='realName!=null'>real_name= #{realName},</if>" +
            "<if test='gender!=null'>gender=#{gender},</if>" +
            "<if test='deleted!=null'>deleted=#{deleted},</if>" +
            "<if test='phone!=null'>phone=#{phone},</if>" +
            "<if test='clazzId!=null'>clazz_id=#{clazz=67tfId},</if>" +
            "<if test='createTime!=null'>create_time=#{createTime},</if>" +
            "<if test='modifyTime!=null'>modify_time=#{modifyTime},</if>" +
            "</set>" +
            " where student_id=#{studentId}" +
            "</script>";

    String insertStudent = "insert into springboot.student(student_id,real_name,gender,phone,deleted,clazz_id,create_time,modify_time) value (#{studentId},#{realName},#{gender},#{phone},#{deleted},#{clazzId},#{createTime},#{modifyTime})";
    /**
     * 按主键查询一条学生记录
     *
     * @param studentId 学生表主键
     * @return 全部学生记录
     */
    @Select(studentList + " where student_id = #{param1}")
    Student selectByStudentId(Integer studentId);
    /**
     * 查询全部学生记录
     *
     * @return 全部学生记录
     */
    @Select(studentList)
    List<Student> studentList();
    /**
     * 添加一条学生记录
     *
     * @param student 学生实体类
     * @return 影响条目数
     */
    @Insert(insertStudent)
    @Options(useGeneratedKeys = true, keyProperty = "studentId")
    int insertStudent(Student student);
    /**
     * 按主键修改一条学生记录
     *
     * @param student 班级实体类
2     * @return 影响条目数
     */
    @Update(updateBystudentId)
    int updateBystudentId(Student student);
    /**
     * 按主键修改一条学生记录
     *
     * @param student 学生实体类
     * @return 影响条目数
     */
    @Delete(deleteBystudentId)
    int deleteBystudentId(Student student);
    /**
     * 查询全部学生记录+clazz部门信息
     *
     * @return 全部学生记录+clazz部门信息
     */
    @Results({
            @Result(id =true,property = "studentId",column = "student_id"),
            @Result(property = "clazzId",column = "clazz_id"),
            @Result(property = "clazz",column = "clazz_id",
                    one = @One(select = "com.lyy.mapper.ClazzMapper.selectByClazzId"))
    })
    @Select(studentList)
    List<Student> listDetail();

    /*定时删除失效的学生*/
    @Delete("delete from springboot.student where deleted=1")
    int deleteInvalidStudent();

}
